package com.template.server.common.result;

/**
 * @author liuxf
 * @date 2024/4/12
 * http响应结果
 */

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
public class R<T> implements Serializable {

    private int code;

    private String message;

    private T data;


    public R(ResultType type) {
        this.code = type.getCode();
        this.message = type.getMessage();
    }

    public R(ResultType type, String message) {
        this.code = type.getCode();
        this.message = message;
    }

    public R(ResultType type, T data) {
        this.code = type.getCode();
        this.message = type.getMessage();
        this.data = data;
    }

    public R(ResultType type, String message, T data) {
        this.code = type.getCode();
        this.message = message;
        this.data = data;
    }

    public static <T> R<T> ok() {
        return new R<T>(ResultType.OK);
    }

    public static <T> R<T> ok(String message) {
        return new R<T>(ResultType.OK, message);
    }

    public static <T> R<T> ok(T data) {
        return new R<T>(ResultType.OK, data);
    }

    public static <T> R<T> fail() {
        return new R<T>(ResultType.FAIL);
    }

    public static <T> R<T> fail(String message) {
        return new R<T>(ResultType.FAIL, message);
    }

    public static <T> R<T> error() {
        return new R<T>(ResultType.ERROR);
    }

    public static <T> R<T> error(int code, String message) {
        R<T> r = new R<>(ResultType.ERROR, message);
        r.setCode(code);
        return r;
    }

    public static <T> R<T> error(String message) {
        return new R<T>(ResultType.ERROR, message);
    }

    public static <T> R<T> notLogin() {
        return new R<T>(ResultType.NOT_LOGIN);
    }

    public static <T> R<T> notPermission() {
        return new R<T>(ResultType.NOT_PERMISSION);
    }

}
